package com.yimi.yinhepda.views.stockCheck_module;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;

import com.pda.ym.base.BaseActivity;
import com.pda.ym.utils.SoundVibratorManager;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.CheckTaskAdapter;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.db.dao.CheckScanTableDao;
import com.yimi.yinhepda.db.dao.StorageInfoTableDao;
import com.yimi.yinhepda.db.table.StorageInfoTable;
import com.yimi.yinhepda.entity.request.CheckTaskRequest;
import com.yimi.yinhepda.entity.request.QueryStorageInfoRequest;
import com.yimi.yinhepda.entity.response.CheckTaskResponse;
import com.yimi.yinhepda.entity.response.QueryStorageInfoResponse;
import com.yimi.yinhepda.event.CountEvent;
import com.pda.ym.net.NetRequest;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.Constants;
import com.yimi.yinhepda.utils.IntentUtils;
import com.pda.ym.utils.SharedPreferUtil;

import java.util.ArrayList;
import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 盘点任务列表界面
 *
 * @author 000432
 */
public class CheckTaskActivity extends BaseActivity {

    //标题栏左边
    @Bind(R.id.title)
    TextView mTvTitle;
    //标题栏中间
    @Bind(R.id.num)
    TextView mTvNum;

    private ListView mListView;
    private List<CheckTaskResponse.CheckTaskEntity> mDataList = new ArrayList<>();
    private CheckTaskAdapter mAdapter;
    private CheckScanTableDao mCheckScanTableDao;
    private String mStorageName = null;

    @Override
    public String getPageName() {
        return getString(R.string.page_check_Task);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_task_list);
        ButterKnife.bind(this);
        initView();
        //        loadData();
    }

    public void initView() {
        mTvTitle.setText("盘点任务列表");
        mListView = findViewById(R.id.check_task_listview);
        findViewById(R.id.check_task_back).setOnClickListener(this);
        findViewById(R.id.check_task_refresh).setOnClickListener(this);
        findViewById(R.id.check_task_execute).setOnClickListener(this);

        EventBus.getDefault().register(this);

        //数据表
        mCheckScanTableDao = new CheckScanTableDao();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //查询未上传数据
        int totalNotUploadNum = mCheckScanTableDao.queryNotUploadDataNum();
        mTvNum.setText(totalNotUploadNum + "");

        loadData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainEventBus(CountEvent countEvent) {
        Log.e("", "event---->" + countEvent.getCount());
        mTvNum.setText(countEvent.getCount() + "");
    }

    /**
     * 访问接口加载数据
     */
    private void loadData() {
        showPgDlg("正在加载...");

        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setResponsClazz(CheckTaskResponse.class)
                .setUrl(Constants.URL_GET_CLEAR_TASK)
                .setRequestObject(new CheckTaskRequest(SharedPreferUtil.getInstance().getValue(Common.USER_CODE)))
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        showToast(msg);
                        dismissPgDlg();
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        if (obj instanceof CheckTaskResponse) {
                            CheckTaskResponse response = (CheckTaskResponse) obj;
                            if (response.data == null || response.data.size() <= 0) {
                                showToast("无盘点任务！");
                                if (mAdapter != null) {
                                    mDataList.clear();
                                    mAdapter.notifyDataSetChanged();
                                }
                            } else {
                                showDataDetail(response);
                            }
                        }
                        dismissPgDlg();
                    }
                });
    }

    /**
     * 数据在列表中展示等
     */
    private void showDataDetail(CheckTaskResponse response) {

        mDataList = response.getData();
        //列表
        mAdapter = new CheckTaskAdapter(this, mDataList, sharedPreferUtil);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.check_task_back:
                finish();
                break;
            case R.id.check_task_refresh:
                loadData();
                break;
            case R.id.check_task_execute:
                if (mAdapter == null) {
                    showToast("暂时没有数据!");
                    return;
                }
                int i = mAdapter.getCurrentPosition();
                if (i != -1) {
                    //获取盘点任务号
                    final String stockNO = mDataList.get(i).getStockNo();
                    mStorageName = "";
                    String deptCode = sharedPreferUtil.getDeptCode();
                    final String storageCode = mDataList.get(i).getStockArea();

                    QueryStorageInfoRequest request = new QueryStorageInfoRequest();
                    request.setDeptCode(deptCode);
                    request.setStorageCode(storageCode);
                    StockCheckUtil.queryStorageInfo(request, new UICallBack() {
                        @Override
                        public void onError(String msg) {
                            showToast(msg);
                        }

                        @Override
                        public void onSuccess(Object obj) {
                            QueryStorageInfoResponse response = (QueryStorageInfoResponse) obj;
                            if (response.getData() != null) {
                                QueryStorageInfoRequest storageInfo = response.getData().get(0);
                                mStorageName = storageInfo.getStorageName();
                            }
                        }
                    });

                    Intent intent = new Intent(CheckTaskActivity.this, CheckScanActivity.class);
                    intent.putExtra(IntentUtils.STOCK_NO, stockNO);
                    intent.putExtra(IntentUtils.STOCK_AREA, mStorageName);
                    intent.putExtra("storageCode", storageCode);
                    startActivity(intent);

                } else {
                    showToast("请选择一项任务执行!");
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }
}
